package com.xinke.module.system.controller.admin.dict;

import com.xinke.component.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.dict.*;
import com.xinke.module.system.model.entity.dict.DictDataDO;
import com.xinke.module.system.service.dict.DictDataService;

@Tag(name = "管理后台 - 字典数据")
@RestController
@RequestMapping("/system/dict-data")
@Validated
public class DictDataController {

    @Resource
    private DictDataService dictDataService;
    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(summary = "获得全部字典数据列表", description = "一般用于管理后台缓存字典数据在本地")
    // 无需添加权限认证，因为前端全局都需要
    public Result<List<DictDataRespVO>> getSimpleDictDataList() {
        List<DictDataDO> list = dictDataService.getDictDataList(
                CommonStatusEnum.ENABLE.getStatus(), null);
        return success(BeanUtils.toBean(list, DictDataRespVO.class));
    }


    @PostMapping("/create")
    @Operation(summary = "创建字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict-data:create')")
    public Result<Long> createDictData(@Valid @RequestBody DictDataSaveReqVO createReqVO) {
        return success(dictDataService.createDictData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict-data:update')")
    public Result<Boolean> updateDictData(@Valid @RequestBody DictDataSaveReqVO updateReqVO) {
        dictDataService.updateDictData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字典数据")
    @Parameter(name = "ddId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:dict-data:delete')")
    public Result<Boolean> deleteDictData(@RequestParam("ddId") Long ddId) {
        dictDataService.deleteDictData(ddId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典数据")
    @Parameter(name = "ddId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dict-data:query')")
    public Result<DictDataRespVO> getDictData(@RequestParam("ddId") Long ddId) {
        DictDataDO dictData = dictDataService.getDictData(ddId);
        return success(BeanUtils.toBean(dictData, DictDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得字典数据分页")
    @PreAuthorize("@ss.hasPermission('system:dict-data:query')")
    public Result<PageResult<DictDataRespVO>> getDictDataPage(@Valid DictDataPageReqVO pageReqVO) {
        PageResult<DictDataDO> pageResult = dictDataService.getDictDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出字典数据 Excel")
    @PreAuthorize("@ss.hasPermission('system:dict-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictDataExcel(@Valid DictDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictDataDO> list = dictDataService.getDictDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "字典数据.xls", "数据", DictDataRespVO.class,
                        BeanUtils.toBean(list, DictDataRespVO.class));
    }

}